package com.yuke.cloud.service.uac.controller.frontend;

import com.yuke.cloud.common.core.mq.RabbitMetaMessage;
import com.yuke.cloud.common.security.core.validate.code.sms.SmsCodeProcessor;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.uac.dto.SmsMessage;
import com.yuke.cloud.service.uac.enums.UacSmsTemplateEnum;
import com.yuke.cloud.service.uac.manager.UserManager;
import com.yuke.cloud.service.uac.mq.producer.SendBatchSms;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * <p>Company: Copyright (c) 2018. yuke All Rights Reserved. </br> </p>
 * <p>Title：yuke-mall </br> </p>
 * <p>com.yuke.cloud.service.uac.controller.frontend Description TODO </br> </p>
 * @Author wg
 * @Date   2019/12/24 19:22
 */
@Api(value = "WEB - AccSendBatchSmsController", tags = "处理AccUser相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Slf4j
@RestController
@RequestMapping("/acc/sms")
public class AccSendBatchSmsController {

    @Resource
    private SendBatchSms sendBatchSms;
    @Resource
    private UserManager userManager;
    @Resource
    private SmsCodeProcessor smsCodeProcessor;

    @PostMapping("/sendBatchSms")
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public void send(String mobile, String templateCode) {

        SmsMessage smsMessage = new SmsMessage();
        smsMessage.setMobileNo(mobile);

        UacSmsTemplateEnum uacSmsTemplateEnum = UacSmsTemplateEnum.getEnum(templateCode);
        if (uacSmsTemplateEnum != null) {
            smsMessage.setSmsTemplateCode(uacSmsTemplateEnum.getTemplateCode());
        }else {
            smsMessage.setSmsTemplateCode(UacSmsTemplateEnum.UAC_SMS_APP_NEW_PRODUCT.getTemplateCode());
        }

        RabbitMetaMessage mqMessageData = sendBatchSms.sendSmsCodeMq(smsMessage);
        try {
            userManager.sendSmsCode(mqMessageData);
        } catch (Exception e) {
            log.info("发送短信失败. error={}", e.getMessage());
        }
    }

    @GetMapping("/getSmsTemplate")
    @ApiOperation(httpMethod = "GET", value = "记录")
    public Wrapper<List<Map<String,String>>> getSmsTemplate() {
        List<Map<String,String>> mapList = new ArrayList<>();
        List<UacSmsTemplateEnum> uacSmsTemplateEnums = UacSmsTemplateEnum.getList();
        for(UacSmsTemplateEnum enu : uacSmsTemplateEnums){
            Map<String,String> map = new HashMap<>();
            map.put("templateCode",enu.getTemplateCode());
            map.put("description",enu.getDescription());
            mapList.add(map);
        }

        return WrapMapper.ok(mapList);
    }
}
